<template>
    <Modal :mask-closable="false" v-model="goodsVisble" :title="`${$t('preliminary.ArrivalDate')}`" width="700" class="modal-content-layout">
        <Form ref="goodsForm" :model="formTop" :rules="ruleGoodsValidate" label-position="top">
            <Row :gutter="24">
                <Col span="12">
                <FormItem :label="`${$t('preliminary.ArrivalDate')}:`" prop="arrivalDate">
                    <DatePicker :value="formTop.arrivalDate" type="date" :placeholder="`${$t('common.selectInfo')}`"
                        @on-change="handleChangeDate" />
                </FormItem>
                </Col>
                <Col span="12">
                <FormItem :label="`${$t('preliminary.ReceivedQuantity')}:`" prop="arrivalNum">
                    <Input type="number" v-model="formTop.arrivalNum" :placeholder="`${$t('common.inputInfo')}`" />
                </FormItem>
                </Col>
            </Row>
        </Form>
        <div slot="footer">
            <Button type="default" @click="handleCancel">{{ $t('common.Cancle') }}</Button>
            <Button type="primary" :loading="loadingVisible" @click="handleOk">{{ $t('maintenance.Confirmed') }}
            </Button>
        </div>
    </Modal>
</template>

<script>
import moment from 'moment';

export default {
    name: "goodsPurchaseModal",
    data() {
        const validateArrivalNum = (rule, value, callback) => {
            if (!value) {
                return callback(new Error(this.$t('preliminary.arrivalQuantityTip')));
            }
            if (!(/(^[1-9]\d*$)/.test(value))) {
                callback(new Error(this.$t('preliminary.arrivalQuantityType')));
            }
            if (this.formTop.arrivalNum < 0 || this.goodsNum < this.formTop.arrivalNum) {
                callback(new Error(this.$t('preliminary.arrivalQuantityMax')));
            } else {
                callback();
            }
        };
        return {
            setFileList: [],
            goodsNum: 0,
            goodsVisble: false,
            loadingVisible: false,
            goodsObj: {},
            formTop: {
                arrivalDate: moment(new Date()).format('YYYY-MM-DD'),
                arrivalNum: '',
            },
            ruleGoodsValidate: {
                arrivalDate: [
                    { required: true, message: this.$t('preliminary.arrivalDateTip'), trigger: 'blur' }
                ],
                arrivalNum: [
                    // { required: true, message: '请填写收货数量', trigger: 'blur' },
                    { validator: validateArrivalNum, trigger: 'blur' }
                ]
            },
            arrvailRecord: []
        }
    },
    watch: {
        formTop(oldValue, newValue) {
            this.formTop.arrivalDate = newValue.arrivalDate
            this.formTop.arrivalNum = newValue.arrivalNum
            console.log(newValue)
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.formTop.arrivalDate = moment(new Date()).format('YYYY-MM-DD')
        })
    },
    methods: {
        show(record) {
            this.goodsVisble = true
            this.goodsObj = { ...record }
            this.goodsNum = record.orderGoodsQuantity-record.arrivalNum
            this.formTop.arrivalNum = record.orderGoodsQuantity-record.arrivalNum
        },
        handleChangeDate(value) {
            console.log(value)
            this.$nextTick(() => {
                this.formTop.arrivalDate = value
            })

        },
        handleOtherDate(value, id) {
            console.log("value", value, id)
            this.arrvailRecord.map(item => {
                if (item.id === id) {
                    item.arrival_date = item.arrivalDate
                    item.arrival_num = item.arrivalNum
                }
            })
        },
        handleOk() {
            //  if( !this.setFileList.length){
            //      this.$Message.warning(({content:"请上传到货凭证",duration:6,closable:true}));
            //      return false
            //  }

            // let firstData = {
            //     arrivalDate: this.formTop.arrivalDate,
            //     arrivalNum: this.formTop.arrivalNum,
            //     orderNo: this.goodsObj.ebeln,
            //     requestNo: this.goodsObj.banfn,
            //     requestProject: this.goodsObj.bnfpo,
            //     orderProject: this.goodsObj.ebelp,
            //     deliveryCertificateList: this.setFileList,
            //     dmsPurchaseId: this.goodsObj.id,
            //     createTime: this.goodsObj.createTime
            // }
            // let firstData = [{
            //     id: 0,
            //     arrivalDate: this.formTop.arrivalDate,
            //     arrivalNum: this.formTop.arrivalNum,
            //     orderNo: this.goodsObj.ebeln,
            //     requestNo: this.goodsObj.banfn,
            //     requestProject: this.goodsObj.bnfpo,
            //     orderProject: this.goodsObj.ebelp,
            // }]
            // let otherData = this.arrvailRecord.map( item => {
            //     return {
            //         id: item.id,
            //         arrivalDate: item.arrival_date,
            //         arrivalNum: item.arrival_num,
            //         orderNo: this.goodsObj.ebeln,
            //         requestNo: this.goodsObj.banfn,
            //         requestProject: this.goodsObj.bnfpo,
            //         orderProject: this.goodsObj.ebelp
            //     }
            // })
            //let dmsDeliveryList = {...firstData, ...otherData,}
            let dmsDeliveryList = { ...this.formTop,id:this.goodsObj.id }
            this.$refs.goodsForm.validate((valid) => {
                if (valid) {
                    this.loadingVisible = true
                    this.axios({
                        method: 'post',
                        url: '/dms/devicePurchase/deviceArrival',
                        data: dmsDeliveryList
                    }).then(res => {
                        if (res.code === 200) {
                            this.goodsVisble = false
                            this.loadingVisible = false
                            this.$Message.success(this.$t('preliminary.arrivalSuccess'))
                            this.$refs.goodsForm.resetFields();
                            this.$emit('refresh')
                        } else {
                            this.goodsVisble = false
                            this.loadingVisible = false
                            this.$Message.error(({ content: res.msg, duration: 6, closable: true }))
                        }
                    })
                }
            }).finally(() => {

            })
        },
        handleCancel() {
            this.$refs.goodsForm.resetFields();
            this.goodsVisble = false
        }
    }

}
</script>

<style scoped>
::v-deep .ivu-upload-drag {
    text-align: left;
}

.form-num {
    display: inline-block;
    border: 1px solid #dcdee2;
    width: 100%;
    height: 32px;
    padding: 0 7px;
    border-radius: 4px;
}

.form-filed {
    border: 1px solid #dcdee2;
    width: 100%;
    padding: 0 7px;
    border-radius: 4px;
}

.form-fileName {
    margin-right: 15px;
    color: #57a3f3;
    cursor: pointer;
}
</style>
